"""
系统概览页面
实时显示系统指标（CPU、内存、磁盘、网络）
"""

from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QGridLayout, 
                             QLabel, QProgressBar, QFrame, QGroupBox)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont


class SystemTab(QWidget):
    """系统概览页面"""
    
    def __init__(self):
        super().__init__()
        self.init_ui()
    
    def init_ui(self):
        """初始化界面"""
        layout = QVBoxLayout()
        layout.setSpacing(20)
        layout.setContentsMargins(20, 20, 20, 20)
        
        # 创建各个信息组
        cpu_group = self.create_cpu_group()
        memory_group = self.create_memory_group()
        disk_group = self.create_disk_group()
        network_group = self.create_network_group()
        
        # 添加到主布局
        layout.addWidget(cpu_group)
        layout.addWidget(memory_group)
        layout.addWidget(disk_group)
        layout.addWidget(network_group)
        layout.addStretch()
        
        self.setLayout(layout)
    
    def create_cpu_group(self) -> QGroupBox:
        """创建 CPU 信息组"""
        group = QGroupBox("CPU 信息")
        group.setFont(QFont("Microsoft YaHei", 10, QFont.Bold))
        
        layout = QGridLayout()
        layout.setSpacing(10)
        
        # CPU 使用率
        layout.addWidget(QLabel("使用率:"), 0, 0)
        self.cpu_usage_label = QLabel("0%")
        self.cpu_usage_label.setFont(QFont("Microsoft YaHei", 12, QFont.Bold))
        self.cpu_usage_label.setStyleSheet("color: #2196F3;")
        layout.addWidget(self.cpu_usage_label, 0, 1)
        
        self.cpu_progress = QProgressBar()
        self.cpu_progress.setRange(0, 100)
        self.cpu_progress.setStyleSheet("""
            QProgressBar {
                border: 2px solid grey;
                border-radius: 5px;
                text-align: center;
            }
            QProgressBar::chunk {
                background-color: #2196F3;
                border-radius: 3px;
            }
        """)
        layout.addWidget(self.cpu_progress, 0, 2, 1, 2)
        
        # CPU 核心数
        layout.addWidget(QLabel("核心数:"), 1, 0)
        self.cpu_cores_label = QLabel("0")
        layout.addWidget(self.cpu_cores_label, 1, 1)
        
        # CPU 频率
        layout.addWidget(QLabel("频率:"), 1, 2)
        self.cpu_freq_label = QLabel("0 MHz")
        layout.addWidget(self.cpu_freq_label, 1, 3)
        
        group.setLayout(layout)
        return group
    
    def create_memory_group(self) -> QGroupBox:
        """创建内存信息组"""
        group = QGroupBox("内存信息")
        group.setFont(QFont("Microsoft YaHei", 10, QFont.Bold))
        
        layout = QGridLayout()
        layout.setSpacing(10)
        
        # 内存使用率
        layout.addWidget(QLabel("使用率:"), 0, 0)
        self.memory_usage_label = QLabel("0%")
        self.memory_usage_label.setFont(QFont("Microsoft YaHei", 12, QFont.Bold))
        self.memory_usage_label.setStyleSheet("color: #4CAF50;")
        layout.addWidget(self.memory_usage_label, 0, 1)
        
        self.memory_progress = QProgressBar()
        self.memory_progress.setRange(0, 100)
        self.memory_progress.setStyleSheet("""
            QProgressBar {
                border: 2px solid grey;
                border-radius: 5px;
                text-align: center;
            }
            QProgressBar::chunk {
                background-color: #4CAF50;
                border-radius: 3px;
            }
        """)
        layout.addWidget(self.memory_progress, 0, 2, 1, 2)
        
        # 内存详情
        layout.addWidget(QLabel("已用:"), 1, 0)
        self.memory_used_label = QLabel("0 GB")
        layout.addWidget(self.memory_used_label, 1, 1)
        
        layout.addWidget(QLabel("总计:"), 1, 2)
        self.memory_total_label = QLabel("0 GB")
        layout.addWidget(self.memory_total_label, 1, 3)
        
        # 虚拟内存
        layout.addWidget(QLabel("虚拟内存:"), 2, 0)
        self.swap_usage_label = QLabel("0%")
        layout.addWidget(self.swap_usage_label, 2, 1)
        
        layout.addWidget(QLabel("虚拟已用:"), 2, 2)
        self.swap_used_label = QLabel("0 GB")
        layout.addWidget(self.swap_used_label, 2, 3)
        
        group.setLayout(layout)
        return group
    
    def create_disk_group(self) -> QGroupBox:
        """创建磁盘信息组"""
        group = QGroupBox("磁盘信息 (C盘)")
        group.setFont(QFont("Microsoft YaHei", 10, QFont.Bold))
        
        layout = QGridLayout()
        layout.setSpacing(10)
        
        # 磁盘使用率
        layout.addWidget(QLabel("使用率:"), 0, 0)
        self.disk_usage_label = QLabel("0%")
        self.disk_usage_label.setFont(QFont("Microsoft YaHei", 12, QFont.Bold))
        self.disk_usage_label.setStyleSheet("color: #FF9800;")
        layout.addWidget(self.disk_usage_label, 0, 1)
        
        self.disk_progress = QProgressBar()
        self.disk_progress.setRange(0, 100)
        self.disk_progress.setStyleSheet("""
            QProgressBar {
                border: 2px solid grey;
                border-radius: 5px;
                text-align: center;
            }
            QProgressBar::chunk {
                background-color: #FF9800;
                border-radius: 3px;
            }
        """)
        layout.addWidget(self.disk_progress, 0, 2, 1, 2)
        
        # 磁盘详情
        layout.addWidget(QLabel("已用:"), 1, 0)
        self.disk_used_label = QLabel("0 GB")
        layout.addWidget(self.disk_used_label, 1, 1)
        
        layout.addWidget(QLabel("可用:"), 1, 2)
        self.disk_free_label = QLabel("0 GB")
        layout.addWidget(self.disk_free_label, 1, 3)
        
        layout.addWidget(QLabel("总计:"), 2, 0)
        self.disk_total_label = QLabel("0 GB")
        layout.addWidget(self.disk_total_label, 2, 1, 1, 3)
        
        group.setLayout(layout)
        return group
    
    def create_network_group(self) -> QGroupBox:
        """创建网络信息组"""
        group = QGroupBox("网络信息")
        group.setFont(QFont("Microsoft YaHei", 10, QFont.Bold))
        
        layout = QGridLayout()
        layout.setSpacing(10)
        
        # 网络速度
        layout.addWidget(QLabel("上传速度:"), 0, 0)
        self.net_upload_label = QLabel("0 KB/s")
        self.net_upload_label.setFont(QFont("Microsoft YaHei", 11, QFont.Bold))
        self.net_upload_label.setStyleSheet("color: #E91E63;")
        layout.addWidget(self.net_upload_label, 0, 1)
        
        layout.addWidget(QLabel("下载速度:"), 0, 2)
        self.net_download_label = QLabel("0 KB/s")
        self.net_download_label.setFont(QFont("Microsoft YaHei", 11, QFont.Bold))
        self.net_download_label.setStyleSheet("color: #9C27B0;")
        layout.addWidget(self.net_download_label, 0, 3)
        
        # 累计流量
        layout.addWidget(QLabel("总上传:"), 1, 0)
        self.net_sent_label = QLabel("0 MB")
        layout.addWidget(self.net_sent_label, 1, 1)
        
        layout.addWidget(QLabel("总下载:"), 1, 2)
        self.net_recv_label = QLabel("0 MB")
        layout.addWidget(self.net_recv_label, 1, 3)
        
        # 数据包统计
        layout.addWidget(QLabel("发送包:"), 2, 0)
        self.net_packets_sent_label = QLabel("0")
        layout.addWidget(self.net_packets_sent_label, 2, 1)
        
        layout.addWidget(QLabel("接收包:"), 2, 2)
        self.net_packets_recv_label = QLabel("0")
        layout.addWidget(self.net_packets_recv_label, 2, 3)
        
        group.setLayout(layout)
        return group
    
    def update_data(self, system_info: dict):
        """更新显示数据"""
        try:
            # 更新 CPU 信息
            cpu_info = system_info.get('cpu', {})
            cpu_usage = cpu_info.get('usage_percent', 0)
            self.cpu_usage_label.setText(f"{cpu_usage:.1f}%")
            self.cpu_progress.setValue(int(cpu_usage))
            self.cpu_cores_label.setText(str(cpu_info.get('core_count', 0)))
            self.cpu_freq_label.setText(f"{cpu_info.get('current_freq', 0):.0f} MHz")
            
            # 更新内存信息
            memory_info = system_info.get('memory', {})
            memory_usage = memory_info.get('usage_percent', 0)
            self.memory_usage_label.setText(f"{memory_usage:.1f}%")
            self.memory_progress.setValue(int(memory_usage))
            self.memory_used_label.setText(f"{memory_info.get('used', 0):.1f} GB")
            self.memory_total_label.setText(f"{memory_info.get('total', 0):.1f} GB")
            self.swap_usage_label.setText(f"{memory_info.get('swap_percent', 0):.1f}%")
            self.swap_used_label.setText(f"{memory_info.get('swap_used', 0):.1f} GB")
            
            # 更新磁盘信息
            disk_info = system_info.get('disk', {})
            disk_usage = disk_info.get('usage_percent', 0)
            self.disk_usage_label.setText(f"{disk_usage:.1f}%")
            self.disk_progress.setValue(int(disk_usage))
            self.disk_used_label.setText(f"{disk_info.get('used', 0):.1f} GB")
            self.disk_free_label.setText(f"{disk_info.get('free', 0):.1f} GB")
            self.disk_total_label.setText(f"{disk_info.get('total', 0):.1f} GB")
            
            # 更新网络信息
            network_info = system_info.get('network', {})
            self.net_upload_label.setText(f"{network_info.get('send_speed', 0):.1f} KB/s")
            self.net_download_label.setText(f"{network_info.get('recv_speed', 0):.1f} KB/s")
            self.net_sent_label.setText(f"{network_info.get('bytes_sent', 0):.1f} MB")
            self.net_recv_label.setText(f"{network_info.get('bytes_recv', 0):.1f} MB")
            self.net_packets_sent_label.setText(f"{network_info.get('packets_sent', 0):,}")
            self.net_packets_recv_label.setText(f"{network_info.get('packets_recv', 0):,}")
            
        except Exception as e:
            print(f"更新系统概览数据失败: {e}")